package com.szholly.plug.form.core.interceptor;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;

/**
 * 类功能说明 定义数据拦截器注册类
 */
public class FormInterceptorRegister {
	
	/**
	 * 编辑拦截
	 */
	public static List<IFormEditInterceptor> dbEditInterceptor = new ArrayList<IFormEditInterceptor>();

	/**
	 * 注册编辑拦截接口
	 * @param editInterceptor
	 */
	public static void RegDbInterceptor(IFormInterceptor editInterceptor) {
		if (editInterceptor == null)
			return;

		if (editInterceptor instanceof IFormEditInterceptor) {
			if (!dbEditInterceptor.contains((IFormEditInterceptor) editInterceptor)) {
				dbEditInterceptor.add((IFormEditInterceptor) editInterceptor);
				Collections.sort(dbEditInterceptor, comparator);
			}
		}
	}

	/**
	 * 移除编辑拦截接口
	 * @param editInterceptor
	 */
	public static void RemoveDbInterceptor(IFormInterceptor editInterceptor) {
		if (editInterceptor == null)
			return;

		if (editInterceptor instanceof IFormEditInterceptor) {
			if (dbEditInterceptor.contains((IFormEditInterceptor) editInterceptor)) {
				dbEditInterceptor.remove((IFormEditInterceptor) editInterceptor);
				Collections.sort(dbEditInterceptor, comparator);
			}
		}
	}

	private static Comparator<IFormInterceptor> comparator = new Comparator<IFormInterceptor>() {
		public int compare(IFormInterceptor a, IFormInterceptor b) {
			if (a.getIndex() - b.getIndex() >= 0) {
				return 1;
			} else {
				return -1;
			}
		}
	};
}
